package com.ruiyian.task;

import com.ruiyian.common.constant.Constants;
import com.ruiyian.common.core.domain.entity.Point;
import com.ruiyian.common.core.redis.RedisCache;
import com.ruiyian.common.utils.jfinsUtil.FinsMessage;
import com.ruiyian.common.utils.jfinsUtil.FinsMessageUtils;
import com.ruiyian.netty.nettyclientTask.NettyStarterTask;
import com.ruiyian.project.equipment.service.PointService;
import com.ruiyian.system.domain.ServerEq;
import com.ruiyian.system.service.ServerService;
import io.netty.channel.ChannelFuture;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author chong
 * @email 673426164@qq.com
 * @create 2022/7/18
 * Desc:
 */
@Slf4j
@Component("nettyTask")
public class MyTask {

    @Autowired
    private PointService pointService;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private ServerService serverService;

    /**
     * 定时任务读取消防补水信息
     */
    public void getFireWater(){
        log.info("消防补水定时任务开始......");
        FinsMessage finsMessage = null;
        try {
            List<Point> pointList = pointService.selectPointList(new Point(0,1));

            if(pointList.size()>0){
                // 统计连接PLC服务状态
                Map<Long, List<Point>> collectMap = pointList.stream().collect(Collectors.groupingBy(p -> p.getServerId()));
                Map<String, ChannelFuture> connectMap = NettyStarterTask.getConnectMap();
                for (Long key : collectMap.keySet()) {
                    ServerEq one = serverService.findOne(key);
                    ChannelFuture channelFuture = connectMap.get(one.getHost() + ":" + one.getPort());
                    if (channelFuture == null || (channelFuture!=null && !channelFuture.isSuccess())) {
                        pointList = pointList.stream().filter(p -> !p.getServerId().equals(key)).collect(Collectors.toList());
                    }
                }
                if(pointList.size()==0){
                    return;
                }
                ChannelFuture channelFuture = connectMap.get(pointList.get(0).getServerPath());
                finsMessage = FinsMessageUtils.createReadFineMessage(pointList.get(0).getMemoryArea(), Integer.parseInt(pointList.get(0).getRegisterAddress()), pointList.get(0).getLength(), pointList.get(0).getServerPath(), Constants.DEFAULT_SA1_T);
                if(null!=channelFuture){
                    // 返回数据
                    redisCache.deleteObject(Constants.RETURNPOINTTASK);
                    redisCache.setCacheObject(Constants.NOWINDEXTASK, 0); // 读取数据下标位
                    // 操作点位集合
                    redisCache.deleteObject(Constants.POINTLISTTASK);
//                    log.info("消防补水定时"+ JSONArray.fromObject(pointList).toString());
                    redisCache.setCacheList(Constants.POINTLISTTASK,pointList);
                    channelFuture.channel().writeAndFlush(finsMessage.toString()).sync();
                }

            }

        } catch (InterruptedException e) {
            log.error(e.getMessage());
        }


    }
}
